﻿using System;
using System.IO;
using Jink.Core;
using Jink.Playground.Domain;
using Jink.Playground.MockData;
using Jink.Playground.MockProtocol;
using Jink.Protocol;
using NUnit.Framework;
using XenoGears.Assertions;

namespace Jink.Playground
{
    [TestFixture]
    public class TestScenarios
    {
        [SetUp]
        public void SetUp()
        {
            if (File.Exists(Database.PdpFileName))
                File.Delete(Database.PdpFileName);
        }

        [Test]
        public void SimpleTest()
        {
            var proto = new JinkProtocol();
            proto.Add(_ => true, new CoreProtocolModule());

            var qbe = JinkMock.Send("/lists?id=pdp/items?user=Burmako, Eugene", "GET");
            (qbe.Count == 2).AssertTrue();
            (qbe[0] == new {
                id = 2,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "PDP webpart development",
                deadline = new DateTime(2010, 02, 26),
                progress = 80.0,
            }).AssertTrue();
            (qbe[1] == new
            {
                id = 3,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "Inventory Tool architecture",
                deadline = new DateTime(2010, 03, 15),
                progress = 0.0,
            }).AssertTrue();

            var update = JinkMock.Send("/lists?id=pdp/items", "POST", new { listItem = "{id : 2, progress : 100}" });
            (update == new
            {
                id = 2,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "PDP webpart development",
                deadline = new DateTime(2010, 02, 26),
                progress = 100.0,
            }).AssertTrue();

            var delete = JinkMock.Send("/lists?id=pdp/items", "DELETE", new { listItem = "{id : 2}" });
            delete.AssertNull();

            var create = JinkMock.Send("/lists?id=pdp/items", "PUT", new { listItem = "{user : 'Burmako, Eugene', goal : 'Conflux implementation', deadline : Date.UTC(2010, 03, 09)}, progress : 70.0}" });
            (create == new
            {
                id = 4,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "Conflux implementation",
                deadline = new DateTime(2010, 03, 09),
                progress = 70.0,
            }).AssertTrue();

            var getall = JinkMock.Send("/lists?id=pdp/items", "GET");
            (getall.Count == 3).AssertTrue();
            (getall[0] == new
            {
                id = 1,
                user = new SPUser { FirstName = "Maksim", LastName = "Matsveyeu" },
                goal = "LINQ training",
                deadline = new DateTime(2008, 12, 01),
                progress = 100.0,
            }).AssertTrue();
            (getall[1] == new
            {
                id = 3,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "Inventory Tool architecture",
                deadline = new DateTime(2010, 03, 15),
                progress = 0.0,
            }).AssertTrue();
            (getall[2] == new
            {
                id = 4,
                user = new SPUser { FirstName = "Eugene", LastName = "Burmako" },
                goal = "Conflux implementation",
                deadline = new DateTime(2010, 03, 09),
                progress = 70.0,
            }).AssertTrue();
        }
    }
}
